﻿using Avalonia.Controls;
using Avalonia.Controls.Notifications;
using Avalonia.Interactivity;
using iOSClub.App.Desktop.ViewModels.Pages;
using iOSClub.App.Desktop.Views;
using iOSClub.App.Share;

namespace iOSClub.App.Desktop.Pages;

public partial class LoginView : UserControl
{
    public LoginView()
    {
        InitializeComponent();
    }

    public LoginView(LoginModel model)
    {
        InitializeComponent();
        
        NameBox.Text = model.Name;
        IdBox.Text = model.Id;
    }

    private async void LoginClick(object? sender, RoutedEventArgs e)
    {
        if (string.IsNullOrEmpty(NameBox.Text) || string.IsNullOrEmpty(IdBox.Text))
        {
            return;
        }

        var view = ViewOpera.GetView<MainWindow>(this);
        if (view == null) return;
        if (await view.Login(new LoginModel() { Name = NameBox.Text, Id = IdBox.Text }))
        {
            view.NotificationShow("LiveHub", "登录成功");
        }
        else
        {
            view.NotificationShow("LiveHub", "登录失败", NotificationType.Error);
        }
    }

    private void SignupClick(object? sender, RoutedEventArgs e)
    {
        var mainWindow = ViewOpera.GetView<MainWindow>(this);
        var a = new SignupPageModel();
        
        mainWindow?.ChangeItem(a.Init());
    }
}